<?php

namespace app\admin\controller;

use app\admin\model\DepartMent;
use app\admin\model\Duty;
use app\oa\logic\User;
use think\Request;
use think\Session;

class AccountController extends BaseController
{
    protected $tableModel = 'oa/User'; //表模型

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //
        $lists = model($this->tableModel)->getAllUser();
        $this->assign('lists', $lists);
        return $this->fetch();
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
       if($this->request->isAjax()){
           $depart = DepartMent::getAllInfo();
           $duty = Duty::getAllInfo();
           $this->assign('depart', $depart);
           $this->assign('duty', $duty);
           return $this->fetch();
       }
    }

    /**
     * @author: yfl
     * @QQ: 554665488
     * @description: ajax 加载部门下的所属人员
     * @time:2017-7-21 13:14:04
     */
    public function ajaxGetUserByDepartId()
    {
        if ($this->request->isAjax()) {
            $id = $this->request->param('id', '', 'intval');
            $departObj = DepartMent::get($id);
            $lists = $departObj->users()->select();
            $html = '';
            if (!empty($lists)) {
                foreach ($lists as $index => $item) {
                    $html = <<<EOF
            <option value='{$item['uid']}'>{$item['realname']}</option>
EOF;
                }
                exit($html);
            } else {
                $html = <<<EOF
            <option value='0'>请选择</option>
EOF;
                exit($html);
            }
        }

    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
        if ($request->isPost()) {
            if ($request->isPost()) {
                $data = $request->post();
                if (User::isRegister($data['mobile'])) {
                    return self::showReturnCodeWithOutData(-1, '该手机号已经注册');
                }
                $res = model($this->tableModel)->allowField(true)->save($data);
                if ($res) {
                    return self::showReturnCodeWithOutData(1, '添加成功');
                } else {
                    return self::showReturnCodeWithOutData(-1, '添加失败');
                }
            } else {
                return self::showReturnCodeWithOutData(404);
            }
        }
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int $id
     * @return \think\Response
     */
    public function edit($id)
    {
        if ($this->request->isGet()) {
            $data = model($this->tableModel)->where(['uid' => $id])->find();
            $data = $data->getData();
            if ($data['upuid'] > 0) {
                $data->upuid_name = model($this->tableModel)->where(['uid' => $data['upuid']])->field('realname')->find()->realname;
            }
            $depart = DepartMent::getAllInfo();
            $duty = Duty::getAllInfo();
            $this->assign('depart', $depart);
            $this->assign('duty', $duty);
            $this->assign('data', $data);
//            halt($data);
            return $this->fetch();
        }
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request $request
     * @param  int $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        if ($request->isPut()) {
            $data = $request->put();
            $res = model($this->tableModel)->allowField(true)->save($data, ['uid' => $id]);
            if (is_numeric($res)) {
                return self::showReturnCodeWithOutData(1, '编辑成功');
            } else {
                return self::showReturnCodeWithOutData(-1, '编辑失败');
            }
        }

    }

    /**
     * 删除指定资源
     *
     * @param  int $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
        if ($this->request->isDelete()) {
            if($id==Session::get('admin_user','admin')->uid){
                return self::showReturnCodeWithOutData(-1, '不能删除');
            }
            $res = model($this->tableModel)->where(['uid' => $id])->delete();
            if ($res > 0) {
                return self::showReturnCodeWithOutData(1, '删除成功');
            } else {
                return self::showReturnCodeWithOutData(-1, '删除失败');
            }
        }

    }
}
